using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace NiMVC
{
    public class ModelManager : MonoBehaviour
    {
        private static ModelManager instance;
        public static ModelManager Instance=>instance;
        private void Awake()
        {
            DontDestroyOnLoad(this.gameObject);
            instance = this;
        }

        [SerializeField]private List<BaseModel> list全数据;

        public List<T> GetCopyDatas<T>() where T : IModelData
        {
            List<T> res = new List<T>();
            foreach (var VARIABLE in list全数据)
            {
                if (VARIABLE is BaseModel<T> data)
                {
                    res = data.GetCopyData();
                }
            }
            
            return res;
        }
        
        public List<T> GetDatas<T>() where T : IModelData
        {
            List<T> res = new List<T>();
            foreach (var VARIABLE in list全数据)
            {
                if (VARIABLE is BaseModel<T> data)
                {
                    res = data.GetData();

                    return res;
                }
            }
            
            return res;
        }
        public T GetOneData<T>(int id) where T : IModelData
        {
            List<T> res = GetDatas<T>();
            foreach (var d in res)
            {
                if (d.GetId() == id)
                {
                    return d;
                }
            }
            
            return default(T);
        }

        public M GetModel<M>()
        {
            foreach (var VARIABLE in list全数据)
            {
                if (VARIABLE is M m)
                {
                    return m;
                }
            }

            return default(M);
        }
    }

    public class ModelAttribute : Attribute
    {
        public string AssetPath;

        public ModelAttribute( string assetPath)
        {
            this.AssetPath = assetPath;
        }
    }


    public class BaseModel<T> : BaseModel where T : IModelData
    {
        [SerializeField] protected List<T> Datas;

        public List<T> GetData()
        {
            return Datas;
        }

        public virtual List<T> GetCopyData()
        {

            return new List<T>();
        }
    }

    public class BaseModel : ScriptableObject,IModel
    {
    }
}